package com.hbu.common.utils;

import lombok.Data;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * @ProjectName: hbu-mall
 * @Package: com.hbu.common.utils
 * @ClassName: TransactionUtils
 * @Author: zxh
 * @Description:
 * @Date: 2022/12/13 19:56
 * @Version: 1.0
 */
public class TransactionUtils {

    public static void doAfterTransaction(TransactionCompletion transactionCompletion) {
        // 判断当前上下文是否有事务被激活
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            TransactionSynchronizationManager.registerSynchronization(transactionCompletion);
        }
    }

    @Data
    public static class TransactionCompletion implements TransactionSynchronization {

        private Runnable runnable;

        @Override
        public void afterCompletion(int status) {
            if (STATUS_COMMITTED == status) {
                runnable.run();
            }
        }

    }

}

